import { generateHighlightNode } from '@/utils/bizUtils';
import { getEchoTextByValue } from '@/utils/getUtils';
import { REPLACE_SPECIAL_SYMBOLS } from '@/constants/regExp';
const IS_INCLUDES_BRAND_OPTIONS = [
  { optionValue: '1', optionHtml: '包含' },
  { optionValue: '2', optionHtml: '不包含' },
]
/**
 * 获取编辑页面的下拉框现值列
 * @param {* 当前列的下拉框的key, 根据key从row中获取options} optionsKey
 * @param {* 当前列的下拉框的option集合,可以是一个数组集合} options
 * @param {* 下拉框change事件回调} changeRunFn
 * @returns {* 现值列}
*/
const getPresentValByGeneralSelect = (getElSelect) => {
  return function ({ optionsKey, options, key, ...args }, changeRunFn = () => {}) {
    return {
      title: '现值',
      key,
      render: (h, { row, index }) => {
        return getElSelect(h, {
          row,
          index,
          key,
          options: optionsKey ? row[optionsKey] : options,
          optionValKey: 'optionValue',
          optionLabelKey: 'optionHtml',
          changeRunFn: () => { changeRunFn(row) },
          ...args
        });
      }
    }
  }
}
/**
 * 获取查看页面的下拉框现值列
 * @param {* 当前列的原值key} oldKey
 * @param {* 当前列的下拉框的key, 根据key从row中获取options} optionsKey
 * @param {* 当前列的下拉框的option集合,可以是一个数组集合} options
 * @returns {* 现值列}
*/
const getViewPresentValBySelect = ({ oldKey, optionsKey, options }) => {
  return {
    title: '现值',
    render: (h, { row }) => {
      return generateHighlightNode(h, row, oldKey, getSelectText({
        val: row[`${oldKey}Update`], options: optionsKey ? row[optionsKey] : options
      }))
    }
  }
}
/**
 * 获取查看页面的文本现值列
 * @param {* 当前列的原值key} oldKey
 * @returns {* 现值列}
*/
const getViewPresentValByText = (oldKey) => {
  return {
    title: '现值',
    render: (h, { row }) => {
      return generateHighlightNode(h, row, oldKey)
    }
  }
}
/**
 * 获取下拉回显文本
 * @param {* 当前下拉值 } val
 * @param {* 当前下拉集合 } options
 * @returns {* 下拉文本回显内容 }
 */
const getSelectText = ({ val, options }) => {
  const target = _.find(options, (item) => {
    return item.optionValue === val;
  });
  if (target) { return target.optionHtml; }
  return val;
}
// 菜单，编辑页面表列
const getTableColumns = ({
  getElInput,
  getElSelect,
  handleChangeByProductType,
  handleInputBlurByKeyword,
  handleInputBlurByProductName,
  renderSeriesSelect
}) => {
  let getSelectPresentVal = getPresentValByGeneralSelect(getElSelect);
  // 原值表列必须有key，用来进行现值的初始化对照
  // 必填项（有required）现值表列必须有key，用来进行必填校验
  const columns = [
    {
      title: 'SPU编码',
      key: 'spuNo'
    },
    {
      title: '商品名称',
      required: true,
      children: [
        {
          title: '原值',
          key: 'productName',
        },
        {
          title: '现值',
          key: 'productNameUpdate',
          render: (h, { row, index }) => {
            return getElInput(h, {
              row, index, key: 'productNameUpdate', blurRunFn: () => { handleInputBlurByProductName(row) }
            });
          }
        },
      ],
    },
    {
      title: '品牌',
      key: 'brandName',
    },
    {
      title: '系列',
      required: true,
      noCheck: true, // 只展示红星，不校验
      children: [
        { title: '原值', key: 'brandseries' },
        {
          title: '现值',
          key: 'brandseriesUpdate',
          render: (h, { row, index }) => renderSeriesSelect(h, { row, index })
        }
      ]
    },
    {
      title: '是否包含品牌',
      required: true,
      children: [
        {
          title: '原值',
          key: 'productNameType',
          render: (h, { row }) => {
            return <span>{getEchoTextByValue({
              val: row.productNameType, firstText: '包含', secondText: '不包含'
            })}</span>
          }
        },
        getSelectPresentVal({
          key: 'productNameTypeUpdate',
          options: IS_INCLUDES_BRAND_OPTIONS,
          disabled: true
        })
      ]
    },
    {
      title: '商品名称（含品牌）',
      required: true,
      children: [
        {
          title: '原值',
          key: 'productNameFinal',
        },
        {
          title: '现值',
          key: 'productNameFinalUpdate',
          render: (h, { row }) => {
            return <span>{generateHighlightNode(h, row, 'productNameFinal')}</span>
          }
        }
      ],
    },
    {
      title: '关键词',
      required: true,
      children: [
        {
          title: '原值',
          key: 'keywordName',
        },
        {
          title: '现值',
          key: 'keywordNameUpdate',
          render: (h, { row }) => {
            return getElInput(h, {
              row,
              key: 'keywordNameUpdate',
              placeholder: '请输入商品关键词',
              maxlength: 20,
              isShowWordLimit: true,
              inputRunFn: ({ val }) => {
                row.keywordNameUpdate = val.replace(REPLACE_SPECIAL_SYMBOLS, '');
              },
              blurRunFn: () => { handleInputBlurByKeyword(row) }
            })
          }
        }
      ],
    },
    {
      title: '助记码',
      children: [
        {
          title: '原值',
          key: 'helpCode',
        },
        {
          title: '现值',
          key: 'helpCodeUpdate',
          render: (h, { row }) => {
            return <span>{generateHighlightNode(h, row, 'helpCode')}</span>
          }
        }
      ],
    },
    {
      title: '商品类型',
      required: true,
      children: [
        {
          title: '原值',
          key: 'productTypeStr',
        },
        getSelectPresentVal(
          { key: 'productTypeUpdate', optionsKey: 'productTypeOptions', width: 130 },
          handleChangeByProductType
        )
      ],
    },
    {
      title: '集散地',
      children: [
        {
          title: '原值',
          key: 'disCentre',
        },
        {
          title: '现值',
          width: 160,
          key: 'disCentreUpdate',
          type: 'input'
        }
      ],
    },
    {
      title: '是否集采',
      required: true,
      children: [
        {
          title: '原值',
          key: 'centralBuyStateStr',
        },
        getSelectPresentVal({
          key: 'centralBuyStateUpdate', optionsKey: 'centralBuyStateOptions', width: 130
        })
      ],
    },
    {
      title: '副标题',
      required: true,
      children: [
        {
          title: '原值',
          key: 'subtitle',
        },
        {
          title: '现值',
          key: 'subtitleUpdate',
          type: 'input',
          width: 160
        }
      ],
    },
    {
      title: '加价类型',
      required: true,
      children: [
        {
          title: '原值',
          key: 'addPriceTypeStr',
        },
        getSelectPresentVal({
          key: 'addPriceTypeUpdate', optionsKey: 'addPriceTypeOptions', width: 130, disabled: true
        })
      ],
    },
    {
      title: '主计量单位',
      key: 'mainUnitStr',
    },
    {
      title: '是否采购多计量',
      key: 'isPurchaseMultiUnitStr',
    },
    {
      title: '是否采销多计量',
      key: 'isSaleMultiUnitStr',
    }
  ]
  getSelectPresentVal = null; // 清空闭包
  return columns;
}

// 查看页面表列
const tableColumnsView = [
  {
    title: 'SPU编码',
    key: 'spuNo'
  },
  {
    title: '商品名称',
    required: true,
    children: [
      {
        title: '原值',
        key: 'productName',
      },
      getViewPresentValByText('productName')
    ],
  },
  {
    title: '品牌',
    key: 'brandName',
  },
  {
    title: '系列',
    required: true,
    noCheck: true, // 只展示红星，不校验
    children: [
      { title: '原值', key: 'brandseries' },
      { title: '现值', key: 'brandseriesUpdate' }
    ]
  },
  {
    title: '是否包含品牌',
    required: true,
    children: [
      {
        title: '原值',
        render: (h, { row }) => {
          return <span>{getEchoTextByValue({
            val: row.productNameType, firstText: '包含', secondText: '不包含'
          })}</span>
        }
      },
      getViewPresentValBySelect({ oldKey: 'productNameType', options: IS_INCLUDES_BRAND_OPTIONS })
    ]
  },
  {
    title: '商品名称（含品牌）',
    required: true,
    children: [
      {
        title: '原值',
        key: 'productNameFinal',
      },
      getViewPresentValByText('productNameFinal')
    ],
  },
  {
    title: '关键词',
    required: true,
    children: [
      {
        title: '原值',
        key: 'keywordName',
      },
      getViewPresentValByText('keywordName')
    ],
  },
  {
    title: '助记码',
    children: [
      {
        title: '原值',
        key: 'helpCode',
      },
      getViewPresentValByText('helpCode')
    ],
  },
  {
    title: '商品类型',
    required: true,
    children: [
      {
        title: '原值',
        key: 'productTypeStr',
      },
      getViewPresentValBySelect({ oldKey: 'productType', optionsKey: 'productTypeOptions' })
    ],
  },
  {
    title: '集散地',
    children: [
      {
        title: '原值',
        key: 'disCentre',
      },
      getViewPresentValByText('disCentre')
    ],
  },
  {
    title: '是否集采',
    required: true,
    children: [
      {
        title: '原值',
        key: 'centralBuyStateStr',
      },
      getViewPresentValBySelect({ oldKey: 'centralBuyState', optionsKey: 'centralBuyStateOptions' })
    ],
  },
  {
    title: '副标题',
    required: true,
    children: [
      {
        title: '原值',
        key: 'subtitle',
      },
      getViewPresentValByText('subtitle')
    ],
  },
  {
    title: '加价类型',
    required: true,
    children: [
      {
        title: '原值',
        key: 'addPriceTypeStr',
      },
      getViewPresentValBySelect({ oldKey: 'addPriceType', optionsKey: 'addPriceTypeOptions' })
    ],
  },
  {
    title: '主计量单位',
    key: 'mainUnitStr',
  },
  {
    title: '是否采购多计量',
    key: 'isPurchaseMultiUnitStr',
  },
  {
    title: '是否采销多计量',
    key: 'isSaleMultiUnitStr',
  }
]
export {
  getTableColumns,
  tableColumnsView
}
